<?php
// +----------------------------------------------------------------------
// | KITEGO-Admin「开箱即用」「人人全栈」
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.kitego.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed KITEGO并不是自由软件，未经许可不能去掉KITEGO相关版权
// +----------------------------------------------------------------------
// | Author: KITEGO Team <bd@kitego.cn>
// +----------------------------------------------------------------------

namespace kitego\extend\throttle;

use Psr\SimpleCache\CacheInterface;

abstract class ThrottleAbstract
{
    protected int $cur_requests = 0; // 当前已有的请求数
    protected int $wait_seconds = 0; // 距离下次合法请求还有多少秒

    /**
     * 是否允许访问
     * @param string $key 缓存键
     * @param float $micronow 当前时间戳,可含毫秒
     * @param int $max_requests 允许最大请求数
     * @param int $duration 限流时长
     * @param CacheInterface $cache 缓存对象
     * @return bool
     */
    abstract public function allowRequest(string $key, float $micronow, int $max_requests, int $duration, CacheInterface $cache): bool;

    /**
     * 计算距离下次合法请求还有多少秒
     * @return int
     */
    public function getWaitSeconds(): int
    {
        return $this->wait_seconds;
    }

    /**
     * 当前已有的请求数
     * @return int
     */
    public function getCurRequests(): int
    {
        return $this->cur_requests;
    }

}